# Copyright (c) 2025, EAM Team and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document


class EquipmentHistory(Document):
    def autoname(self):
        """自动生成履历编号"""
        equipment_code = frappe.get_value("Equipment", self.equipment, "equipment_code")
        count = frappe.db.count("Equipment History", {"equipment": self.equipment}) + 1
        self.name = f"HIST-{equipment_code}-{count:04d}"

    def validate(self):
        """验证数据"""
        if not self.record_date:
            self.record_date = frappe.utils.now()
        
        if not self.operator:
            self.operator = frappe.session.user